package com.bw.unit3.view.fragment

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.bw.unit3.Const
import com.bw.unit3.R
import com.bw.unit3.base.BaseFragment
import com.bw.unit3.bean.TypeBean
import com.bw.unit3.bean.TypeGoodsBean
import com.bw.unit3.databinding.FragmentTypeBinding
import com.bw.unit3.view.adapter.TypeAdapter
import com.bw.unit3.view.adapter.TypeGoodsAdapter
import com.bw.unit3.viewmodel.TypeViewModel
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter.OnItemClickListener

/**
 * create an instance of this fragment.
 */
class TypeFragment : BaseFragment<TypeViewModel, FragmentTypeBinding>(R.layout.fragment_type) {

    //类型适配器
    private lateinit var typeAdapter: TypeAdapter

    //商品适配器
    private lateinit var goodsAdapter: TypeGoodsAdapter
    val typeList = mutableListOf<TypeBean>()
    val goodsList = mutableListOf<TypeGoodsBean>()
    override fun initData() {
        //接收 分类列表数据
        viewModel.typeLiveData.observe(this) {
            if (it.data.isNotEmpty()) {
                it.data[0].isCheck = true
                typeList.clear()
                typeList.addAll(it.data)
                typeAdapter.notifyDataSetChanged()

                //设置默认显示的商品列表
                goodsList.clear()
                goodsList.addAll(typeList[0].data)
                goodsAdapter.notifyDataSetChanged()

            }
        }
    }

    override fun initView() {
        viewModel.getTypeGoods()

        //初始化类型适配器
        dataBinding.rvType.layoutManager = LinearLayoutManager(activity)
        typeAdapter = TypeAdapter(activity, R.layout.item_type_layout, typeList)
        dataBinding.rvType.adapter = typeAdapter
        typeAdapter.setOnItemClickListener(object : MultiItemTypeAdapter.OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, position: Int) {
                //设置item是否选中
                for (i in typeList.indices) {
                    if (i == position) {
                        typeList[i].isCheck = true
                    } else {
                        typeList[i].isCheck = false
                    }
//                    typeList[i].isCheck = i == position
                }
                //刷新适配器，改变item选中颜色
                typeAdapter.notifyDataSetChanged()

                //每次点击分类列表，则给商品列表赋值，并刷新goodsAdapter适配器
                goodsList.clear()
                //获取选中分类的商品列表
                goodsList.addAll(typeList[position].data)
                goodsAdapter.notifyDataSetChanged()
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                return false
            }
        })

        //初始化goodsAdapter
        dataBinding.rvGoods.layoutManager = GridLayoutManager(activity, 3)
        goodsAdapter = TypeGoodsAdapter(activity, R.layout.item_type_goods_layout, goodsList)
        dataBinding.rvGoods.adapter = goodsAdapter

        goodsAdapter.setOnItemClickListener(object : OnItemClickListener {
            override fun onItemClick(p0: View?, p1: RecyclerView.ViewHolder?, p2: Int) {
                ARouter.getInstance().build(Const.PATH_GOODS_DETAIL)
                    .withInt("goodsId", goodsList[p2].id)
                    .navigation()
            }

            override fun onItemLongClick(
                p0: View?,
                p1: RecyclerView.ViewHolder?,
                p2: Int
            ): Boolean {
                TODO("Not yet implemented")
            }

        })
    }


}